#!/bin/sh
#
#

# PROVIDE: ix-localtime
# REQUIRE: FILESYSTEMS
# BEFORE: NETWORKING devd

. /etc/rc.subr

#
# Set up timezone
#
install_timezone()
{
	local IFS="|"
	local f="stg_timezone"
	eval local $f
	local sf=$(var_to_sf $f)
	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT
	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "SELECT $sf FROM system_settings ORDER BY -id LIMIT 1" | \
	while eval read $f; do
		timezone=$(echo ${stg_timezone} | sed -es=america-los_angeles=America/Los_Angeles=)
		if [ -z "${stg_timezone}" ]; then
			stg_timezone=America/Los_Angeles
		fi
		cp /usr/share/zoneinfo/${stg_timezone} /etc/localtime
		echo ${stg_timezone} > /var/db/zoneinfo
	done
}

name="ix-localtime"
start_cmd='install_timezone'
stop_cmd=':'

load_rc_config $name
run_rc_command "$1"
